﻿using Sunboxsoft.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sunboxsoft.ASMS.Server.Domain.Service
{
    class InlineCreateService
    {
        public static ServiceResult CreateStockDecreaseRecord(ASMSDbContext dbContext,
            DateTime now,
            decimal outPrice,
            decimal outCount,
            string handler,
            int billType,
            Guid outWarehouseId,
            Guid outMaterialId,
            Guid billId,
            string billNumber)
        {
            Entity.EStock stock = dbContext.Set<Entity.EStock>().Where(p => p.WarehouseId == outWarehouseId && p.MaterialId == outMaterialId).SingleOrDefault();

            if (stock == null)
            {
                return new ServiceResult(1, "not enough stock");
            }
            else
            {
                stock.Cost -= outPrice * outCount;
                stock.Count -= outCount;
                if (stock.Count == 0)
                {
                    stock.AverageCost = stock.Cost;
                }
                else
                {
                    stock.AverageCost = stock.Cost / stock.Count;
                }
                stock.UpdatedTime = now;
                stock.Updator = handler;
                dbContext.Entry<Entity.EStock>(stock).State = System.Data.Entity.EntityState.Modified;
            }
            // 生成库存变更记录
            Entity.EStockChangedRecord changeRecord = new Entity.EStockChangedRecord();
            changeRecord.Id = Guid.NewGuid();
            changeRecord.Type = billType;
            changeRecord.WarehouseId = outWarehouseId;
            changeRecord.MaterialId = outMaterialId;
            changeRecord.BillId = billId;
            changeRecord.BillNumber = billNumber;
            changeRecord.BillType = Constant.BillTypes.StockDecreaseBill;
            changeRecord.StockId = stock.Id;
            changeRecord.Cost = outPrice * outCount;
            changeRecord.Count = outCount;
            changeRecord.RemainingCost = stock.Cost;
            changeRecord.RemainingCount = stock.Count;
            changeRecord.CreatedTime = now;
            changeRecord.Creator = handler;
            dbContext.Set<Entity.EStockChangedRecord>().Add(changeRecord);
            return new ServiceResult();
        }

        public static ServiceResult CreateStockIncreaseRecord(ASMSDbContext dbContext,
            DateTime now,
            decimal inCost,
            decimal inCount,
            string handler,
            int billType,
            Guid inWarehouseId,
            Guid inMaterialId,
            Guid billId,
            string billNumber)
        {
            Entity.EStock stock = dbContext.Set<Entity.EStock>().Where(p => p.WarehouseId == inWarehouseId && p.MaterialId == inMaterialId).SingleOrDefault();

            if (stock == null)
            {
                // 创建库存信息
                stock = new Entity.EStock();
                stock.Id = Guid.NewGuid();
                stock.WarehouseId = inWarehouseId;
                stock.MaterialId = inMaterialId;
                stock.Cost = inCost;
                stock.Count = inCount;
                if (stock.Count == 0)
                {
                    stock.AverageCost = stock.Cost;
                }
                else
                {
                    stock.AverageCost = stock.Cost / stock.Count;
                }
                stock.UpdatedTime = now;
                stock.Updator = handler;
                dbContext.Set<Entity.EStock>().Add(stock);
            }
            else
            {
                stock.Cost += inCost * inCount;
                stock.Count += inCount;
                if (stock.Count == 0)
                {
                    stock.AverageCost = stock.Cost;
                }
                else
                {
                    stock.AverageCost = stock.Cost / stock.Count;
                }
                stock.UpdatedTime = now;
                stock.Updator = handler;
                dbContext.Entry<Entity.EStock>(stock).State = System.Data.Entity.EntityState.Modified;
            }
            // 生成库存变更记录
            Entity.EStockChangedRecord changeRecord = new Entity.EStockChangedRecord();
            changeRecord.Id = Guid.NewGuid();
            changeRecord.Type = billType;
            changeRecord.WarehouseId = inWarehouseId;
            changeRecord.MaterialId = inMaterialId;
            changeRecord.BillId = billId;
            changeRecord.BillNumber = billNumber;
            changeRecord.BillType = Constant.BillTypes.StockIncreaseBill;
            changeRecord.StockId = stock.Id;
            changeRecord.Cost = inCost * inCount;
            changeRecord.Count = inCount;
            changeRecord.RemainingCost = stock.Cost;
            changeRecord.RemainingCount = stock.Count;
            changeRecord.CreatedTime = now;
            changeRecord.Creator = handler;
            dbContext.Set<Entity.EStockChangedRecord>().Add(changeRecord);
            return new ServiceResult();
        }

        /// <summary>
        /// 创建资金入账记录
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="now"></param>
        /// <param name="handler"></param>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="changedType"></param>
        /// <param name="capitalRoleType"></param>
        /// <param name="capitalRole"></param>
        /// <param name="amount"></param>
        /// <param name="billId"></param>
        /// <param name="billType"></param>
        /// <param name="billNumber"></param>
        public static void CreateCapitalIncreaseRecord(ASMSDbContext dbContext,
            DateTime now,
            string handler,
            Guid businessId,
            Guid stationId,
            int changedType,
            int capitalRoleType,
            string capitalRole,
            decimal amount,
            Guid billId,
            int billType,
            string billNumber)
        {
            // 生成资金变更记录
            Entity.ECapitalChangedRecord capitalChangedRecord = new Entity.ECapitalChangedRecord();
            capitalChangedRecord.Id = Guid.NewGuid();
            capitalChangedRecord.BusinessId = businessId;
            capitalChangedRecord.StationId = stationId;
            capitalChangedRecord.ChangedType = changedType;
            capitalChangedRecord.CapitalRole = capitalRole;
            capitalChangedRecord.CapitalRoleType = capitalRoleType;
            capitalChangedRecord.Amount = amount;
            capitalChangedRecord.BillId = billId;
            capitalChangedRecord.BillNumber = billNumber;
            capitalChangedRecord.BillType = Constant.BillTypes.CapitalIncreaseBill;
            capitalChangedRecord.CreatedTime = now;
            capitalChangedRecord.Creator = handler;
            dbContext.Set<Entity.ECapitalChangedRecord>().Add(capitalChangedRecord);
        }
    }
}
